package PVZ;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

public class World extends JPanel {
    private static final int WIDTH = 900;
    private static final int HEIGHT = 600;
    private static final int MAP0=0;
    private static final int MAP1=1;
    private static final int MAP2=2;
    private static final int MAP3=3;
    private static int worldstate=MAP3;

    MapRole mapRole=new MapRole();
    Title title=new Title();
    //僵尸数组
    private Senobject[] zombie = {
            new BucketheadZombie(),
            new ConeheadZombie(),
            new Flagpolezombie(),
            new NewspaperZombie(),
            new Zombie()
    };
    //植物数组
    private Senobject[] plants = {
            new Repeater(280,150),
            new Snowpea(280,150),
            new Peashooter(280,450)
    };
    //子弹数组
    private Senobject[] bullets = {};

    //子弹入场
    private int bulletEnterIndex = 0;
    public void BulletEnterIndexAction(){
        bulletEnterIndex++;
        if (bulletEnterIndex%100==0){
            for (int i=0;i<plants.length;i++){
                Senobject obj = plants[i].shootBullet();
                if (obj!=null){
                    bullets = Arrays.copyOf(bullets,bullets.length+1);
                    bullets[bullets.length-1] = obj;
                }
            }
        }
    }

    //删除对象
    public void deleteworld(){
        for (int i = 0; i < bullets.length; i++) {
            if (!bullets[i].isLive()){
                bullets[i] = bullets[bullets.length-1];
                bullets = Arrays.copyOf(bullets,bullets.length-1);
            }
        }
    }
    //子弹与僵尸碰撞
    public void Bulletcollideswithzombie(){
        for (int i = 0; i < zombie.length; i++) {
           Senobject z = zombie[i];
            for (int j = 0; j < bullets.length; j++) {
                Senobject b = bullets[j];
                if (z.isLive() && b.isLive() && z.isHit(b)){
                    z.loseLife();
                    b.goDead();
                }
            }
        }
    }

    //僵尸与植物碰撞

    /*启动程序的执行*/
    public void action(){
        Timer timer = new Timer();
        int intervel = 10;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Bulletcollideswithzombie();
                BulletEnterIndexAction();
                mapRole.Xrught();
                if (mapRole.kill()){
                    worldstate=MAP3;
                }
                deleteworld();
                repaint();
            }
        }, intervel, intervel);

    }

    /*重写paint()画  g:画笔*/
    public void paint(Graphics g) {
        switch (worldstate){
            case MAP0:
                Images.welcome.paintIcon(null,g,0,0);
                break;
            case MAP1:
                title.paintComponent(g);
                break;
            case MAP2:
                mapRole.paintImage(g);
                break;
            case MAP3:
                Images.background.paintIcon(null, g, -120, 0);
//                Images.Repeater.paintIcon(null,g,140,171+92*0-71);
//                Images.Peashooter.paintIcon(null,g,140,100+92*1);
//                Images.Peashooter.paintIcon(null,g,140,100+92*2);
//                Images.Peashooter.paintIcon(null,g,140,100+92*3);
//                Images.Peashooter.paintIcon(null,g,140,100+92*4);
//                Images.Peashooter.paintIcon(null,g,140+80*1,100);
//                Images.Peashooter.paintIcon(null,g,140+80*2,100);
//                Images.Peashooter.paintIcon(null,g,140+80*3,100);
//                Images.Peashooter.paintIcon(null,g,140+80*4,100);
//                Images.Peashooter.paintIcon(null,g,140+80*5,100);
//                Images.Peashooter.paintIcon(null,g,140+80*6,100);
//                Images.Peashooter.paintIcon(null,g,140+80*7,100);
//                Images.Peashooter.paintIcon(null,g,140+80*8,100);
                for (int i = 0; i < zombie.length; i++) {
                    zombie[i].paintImage(g);
                    zombie[i].step();
                }
                for (int i=0;i < plants.length;i++){
                    plants[i].paintImage(g);
                }
                for (int i=0;i < bullets.length;i++){
                    bullets[i].paintImage(g);
                    bullets[i].step();
                }
                break;
        }



    }


    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame();
        World world = new World();
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(3);
        frame.setSize(WIDTH+16,HEIGHT+39);
        frame.setLocationRelativeTo((Component) null);
        frame.setVisible(true);

        world.action();

    }
}